﻿using ASmile.Com;
using ASmile.DBModel;
using System;
using System.Windows.Forms;

namespace ASmile.Win.Forms
{
    public partial class FrmCfgDict : FrmBase
    {
        public FrmCfgDict()
        {
            InitializeComponent();
        }

        ICfgDictBLL BLLObj { get; } = ObjectFactory.GetObject<ICfgDictBLL>("CfgDictBLL");

        private void FrmCfgDict_Load(object sender, EventArgs e)
        {
            tsBtnTypeRefrash_Click(sender, e);
            SetCopyInsertSql<Cfg_Dict>(dgvCfg);
        }

        private void tsBtnTypeRefrash_Click(object sender, EventArgs e)
        {
            var rList = BLLObj.GetCfgDictType().data;
            dgvType.DataSource = rList;
        }

        private void tsBtnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void tsBtnRefreshCfg_Click(object sender, EventArgs e)
        {
            var r = dgvType.GetSelectedClassData<Cfg_DictType>();
            if (r == null)
            {
                dgvCfg.DataSource = null;
                return;
            }

            var rList = BLLObj.GetCfgDicts(r.Id).data;
            dgvCfg.DataSource = rList;
        }

        private void tsBtnEdit_Click(object sender, EventArgs e)
        {
            var cfg = dgvCfg.GetSelectedClassData<Cfg_Dict>();
            FrmCfgDictEdit frm = new FrmCfgDictEdit();
            var ret =
            frm.ShowDialog(new EditFormArgs
            {
                SetModel = cfg,
                SaveHandle = (m) => BLLObj.SaveCfgValue(m as Cfg_Dict)
            });

            if (ret)
            {
                ShowMessage("保存成功");
                dgvCfg.Refresh();
            }
        }

        private void dgvType_SelectionChangedAndCellClick(object sender, DataGridViewCellEventArgs e)
        {
            tsBtnRefreshCfg_Click(sender, e);
        }
    }
}